package top.lyjwn.ajui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import top.lyjwn.ajui.R;
import top.lyjwn.ajui.widget.banner.BannerItem;
import top.lyjwn.ajui.widget.banner.BannerView;
import top.lyjwn.ajui.widget.image.NetImageView;

/**
 * 轮播图适配器
 *
 * @author luoyujie
 */
public class BannerDoubleAdapter extends RecyclerView.Adapter<BannerDoubleAdapter.BannerItemHolder> implements BannerAdapter {

    private List<BannerItem> urls;
    private Context context;
    private BannerView.OnClickItemListener listener;


    public BannerDoubleAdapter(Context context, BannerView.OnClickItemListener listener) {
        this.urls = new ArrayList<>();
        this.context = context;
        this.listener = listener;
    }

    @Override
    public BannerItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.widget_banner_2image_item, parent, false);
        return new BannerItemHolder(view);
    }

    @Override
    public void onBindViewHolder(BannerItemHolder holder, int position) {
        final int i = position % urls.size();
        holder.item_image1.load(urls.get(i).getImgUrl());
        holder.item_image2.load(urls.get(i + 1 >= urls.size() ? 0 : i + 1).getImgUrl());
        //转换成String
        final List<String> urls_str = new ArrayList<>();
        for (BannerItem bannerItem : urls) {
            urls_str.add(bannerItem.getImgUrl());
        }
        //预览
        if (listener == null) {
            holder.item_image1.preview(urls_str, i);
            if (urls_str.size() > 1) {
                holder.item_image2.preview(urls_str, i + 1 >= urls_str.size() ? 0 : i + 1);
            }
        } else {
            holder.item_image1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onItemClick(urls.get(i), i);
                }
            });
            if (urls_str.size() > 1) {
                holder.item_image2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        listener.onItemClick(urls.get(i + 1 >= urls_str.size() ? 0 : i + 1), i + 1 >= urls_str.size() ? 0 : i + 1);
                    }
                });
            }

        }

    }

    @Override
    public int getItemCount() {
        return Integer.MAX_VALUE;
    }

    public class BannerItemHolder extends RecyclerView.ViewHolder {
        private NetImageView item_image1;
        private NetImageView item_image2;
        private LinearLayout item_image2_container;

        public BannerItemHolder(View itemView) {
            super(itemView);
            item_image1 = itemView.findViewById(R.id.item_image1);
            item_image2 = itemView.findViewById(R.id.item_image2);
            item_image2_container = itemView.findViewById(R.id.item_image2_container);
            if (urls.size() == 1) {
                item_image2_container.setVisibility(View.GONE);
            }
        }
    }

    public void refresh(List<? extends BannerItem> urls) {

        if (this.urls!=null){
            for (int i = 0; i < this.urls.size(); i++) {
                removeItem();
            }
        }

        for (int i = 0; i < urls.size(); i++){
            this.urls.add(urls.get(i));
            //添加动画
            notifyItemInserted(i);
        }
    }

    //  删除数据
    public void removeItem() {
        urls.clear();
        //删除动画
        notifyDataSetChanged();
    }

    @Override
    public List<BannerItem> getItems() {
        return urls;
    }

}
